package org.yh.moodpraise.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.yh.moodpraise.dao.UserMoodPraiseRelMapper;

import java.util.Set;

/**
 * @Author: yh
 * @Date: 2020/6/21
 * @Description:
 */

@Service
public class UserMoodPraiseRelService {
    //项目名称+模块+被点赞的mood的id
    private static final String PRAISE_HASH_KEY = "moodpraise.praise.moodid";
    @Autowired
    UserMoodPraiseRelMapper userMoodPraiseRelMapper;
    @Autowired
    RedisTemplate redisTemplate;

    public int addPraiseInfo(Integer userId, Integer moodId) {
        return userMoodPraiseRelMapper.addPraiseInfo(userId, moodId);
    }

    //点赞信息存入redis
    public void addPraiseInfoForRedis(Integer userId, Integer moodId) {
        //存放说说种类(不同id)
        redisTemplate.opsForSet().add(PRAISE_HASH_KEY, moodId);
        Set members = redisTemplate.opsForSet().members(PRAISE_HASH_KEY);
        System.out.println(members);
        //存放说说被点赞的用户id（去重，舍去同一个用户对某个说说重复点赞）
        redisTemplate.opsForSet().add(moodId, userId);
        Set members2 = redisTemplate.opsForSet().members(PRAISE_HASH_KEY);
        System.out.println(members2);
    }
}
